﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartApplication.Api.Applient.Command;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.Api.Applient.Command.Source;
using SmartApplication.Api.Dto;
using SmartApplication.ApiCode;
using System.Security.Claims;

namespace SmartApplication.Api.Controllers
{
    /// <summary>
    /// 溯源信息控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class SourceController : ControllerBase
    {
        public SourceController(IMediator mediator)
        {
            Mediator = mediator;
        }

        public IMediator Mediator { get; }

        /// <summary>
        /// 显示溯源数据信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiPaging<List<SourceDto>>> ListSource([FromQuery]ListSourceCommand request)
        {
            return await Mediator.Send(request);
        }


        /// <summary>
        /// 添加溯源基础数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> AddSource(AddSourceCommand request)
        {
            var accountName = this.User.FindFirstValue("accountName");
            request.AccountName = accountName;
            return await Mediator.Send(request);
        }

        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<string>> Upload(IFormFile file)
        {
            var result = new ApiResult<string>();

            var ins=DateTime.Now.Ticks.ToString();
            var filename="./wwwroot/pic/"+ins+"_"+file.FileName;

            using (var fs=new FileStream(filename,FileMode.Create))
            {
               file.CopyTo(fs);
                fs.Flush();
            }
            var path = Request.Scheme + "://" + Request.Host + "/pic/" + ins + "_" + file.FileName;
            result.Code = EnumCode.Success;
            result.Message = "添加图片成功";
            result.Data = path;
            return result;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<LoginDto>> LoginAccount(LoginAccountCommand request)
        {
            return await Mediator.Send(request);
        }

        /// <summary>
        /// 溯源信息逻辑删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> DeleteSource(DeleteSourceCommand request)
        {
            return await Mediator.Send(request);
        }

        /// <summary>
        /// 修改溯源基础数据
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> UpdateSource(UpdateSourceCommand request)
        {
            return await Mediator.Send(request);
        }


        /// <summary>
        /// 批量逻辑删除溯源信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> BatchDeleteSource(BatchDeleteSourceCommand request)
        {
            return await Mediator.Send(request);
        }
    }
}
